package presentation;

import java.io.IOException;
import java.sql.Date;
import java.util.Calendar;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import domain.EmpresaPublica;
import domain.Proyecto;

/**
 * Servlet implementation class for Servlet: InsertarProyecto
 *
 */
 public class InsertarProyecto extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public InsertarProyecto() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}  	
	
	

	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	} 
	private void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession sesion =  request.getSession();
		int id =  (Integer)sesion.getAttribute("session.id");
		Proyecto p = new Proyecto();
		p.setEpId(id);
		
		p.setDescripcion(request.getParameter("descripcion"));
		
		p.setNombre(request.getParameter("nombre"));
		//System.out.println(request.getParameter("dia")+"-"+request.getParameter("mes")+"-"+request.getParameter("ano"));
		String dia = request.getParameter("dia");
		String mes = request.getParameter("mes");
		String año = request.getParameter("ano");
		java.sql.Date fechaFin = java.sql.Date.valueOf(año+"-"+mes+"-"+dia);
		p.setFechaFin(fechaFin);
		EmpresaPublica ep = new EmpresaPublica();
		String resource = "./error/error.jsp";
		if(ep.crearProyecto(p)){
			resource ="./EmpresaPublica/AgregarTareasAProyecto/elegirProyecto.jsp";
			RequestDispatcher d = request.getRequestDispatcher(resource);
			if(d!=null){
				d.forward(request,response);
			}
		}else{
			response.sendRedirect(resource);
		}
		
		//
		
		
	}
}